
package com.rt.schedulenew.utils.coder;

import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.math.BigInteger;

public abstract class HmacCoder extends SecurityCoder
{
    public static byte[] initHmacMD5Key() throws Exception {
        final KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacMD5");
        final SecretKey secretKey = keyGenerator.generateKey();
        return secretKey.getEncoded();
    }
    
    public static byte[] encodeHmacMD5(final byte[] data, final byte[] key) throws Exception {
        final SecretKey secretKey = new SecretKeySpec(key, "HmacMD5");
        final Mac mac = Mac.getInstance("SslMacMD5");
        mac.init(secretKey);
        return mac.doFinal(data);
    }
    
    public static byte[] initHmacSHAKey() throws Exception {
        final KeyGenerator keyGenerator = KeyGenerator.getInstance("HMacTiger");
        final SecretKey secretKey = keyGenerator.generateKey();
        return secretKey.getEncoded();
    }
    
    public static byte[] encodeHmacSHA(final byte[] data, final byte[] key) throws Exception {
        final SecretKey secretKey = new SecretKeySpec(key, "HMacTiger");
        final Mac mac = Mac.getInstance("SslMacMD5");
        mac.init(secretKey);
        return mac.doFinal(data);
    }
    
    public static byte[] initHmacSHA256Key() throws Exception {
        final KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacSHA256");
        final SecretKey secretKey = keyGenerator.generateKey();
        return secretKey.getEncoded();
    }
    
    public static byte[] encodeHmacSHA256(final byte[] data, final byte[] key) throws Exception {
        final SecretKey secretKey = new SecretKeySpec(key, "HmacSHA256");
        final Mac mac = Mac.getInstance(secretKey.getAlgorithm());
        mac.init(secretKey);
        return mac.doFinal(data);
    }
    
    public static byte[] initHmacSHA384Key() throws Exception {
        final KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacSHA384");
        final SecretKey secretKey = keyGenerator.generateKey();
        return secretKey.getEncoded();
    }
    
    public static byte[] encodeHmacSHA384(final byte[] data, final byte[] key) throws Exception {
        final SecretKey secretKey = new SecretKeySpec(key, "HmacSHA384");
        final Mac mac = Mac.getInstance(secretKey.getAlgorithm());
        mac.init(secretKey);
        return mac.doFinal(data);
    }
    
    public static byte[] initHmacSHA512Key() throws Exception {
        final KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacSHA512");
        final SecretKey secretKey = keyGenerator.generateKey();
        return secretKey.getEncoded();
    }
    
    public static byte[] encodeHmacSHA512(final byte[] data, final byte[] key) throws Exception {
        final SecretKey secretKey = new SecretKeySpec(key, "HmacSHA512");
        final Mac mac = Mac.getInstance(secretKey.getAlgorithm());
        mac.init(secretKey);
        return mac.doFinal(data);
    }
    
    public static byte[] initHmacMD2Key() throws Exception {
        final KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacMD2");
        final SecretKey secretKey = keyGenerator.generateKey();
        return secretKey.getEncoded();
    }
    
    public static byte[] encodeHmacMD2(final byte[] data, final byte[] key) throws Exception {
        final SecretKey secretKey = new SecretKeySpec(key, "HmacMD2");
        final Mac mac = Mac.getInstance(secretKey.getAlgorithm());
        mac.init(secretKey);
        return mac.doFinal(data);
    }
    
    public static String encodeHmacMD2Hex(final byte[] data, final byte[] key) throws Exception {
        final byte[] b = encodeHmacMD2(data, key);
        return new String(Hex.encode(b));
    }
    
    public static byte[] initHmacMD4Key() throws Exception {
        final KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacMD4");
        final SecretKey secretKey = keyGenerator.generateKey();
        return secretKey.getEncoded();
    }
    
    public static byte[] encodeHmacMD4(final byte[] data, final byte[] key) throws Exception {
        final SecretKey secretKey = new SecretKeySpec(key, "HmacMD4");
        final Mac mac = Mac.getInstance(secretKey.getAlgorithm());
        mac.init(secretKey);
        return mac.doFinal(data);
    }
    
    public static String encodeHmacMD4Hex(final byte[] data, final byte[] key) throws Exception {
        final byte[] b = encodeHmacMD4(data, key);
        return new String(Hex.encode(b));
    }
    
    public static byte[] initHmacSHA224Key() throws Exception {
        final KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacSHA224");
        final SecretKey secretKey = keyGenerator.generateKey();
        return secretKey.getEncoded();
    }
    
    public static byte[] encodeHmacSHA224(final byte[] data, final byte[] key) throws Exception {
        final SecretKey secretKey = new SecretKeySpec(key, "HmacSHA224");
        final Mac mac = Mac.getInstance(secretKey.getAlgorithm());
        mac.init(secretKey);
        return mac.doFinal(data);
    }
    
    public static String encodeHmacSHA224Hex(final byte[] data, final byte[] key) throws Exception {
        final byte[] b = encodeHmacSHA224(data, key);
        return new String(Hex.encode(b));
    }
    
    public static void main(final String[] args) {
        try {
            final BigInteger integer = new BigInteger(1, initHmacSHA256Key());
            System.out.println(integer.toString(16));
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}
